package com.childenglish.config;

import com.childenglish.filter.XSSFilter;
import com.childenglish.filter.CSRFFilter;
import com.childenglish.filter.RateLimitFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 安全配置类
 * 配置XSS防护、CSRF防护、DoS防护等安全过滤器
 */
@Configuration
public class SecurityConfig {

    /**
     * XSS防护过滤器
     */
    @Bean
    public FilterRegistrationBean<XSSFilter> xssFilter() {
        FilterRegistrationBean<XSSFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new XSSFilter());
        registration.addUrlPatterns("/*");
        registration.setName("xssFilter");
        registration.setOrder(1);
        return registration;
    }

    /**
     * CSRF防护过滤器
     */
    @Bean
    public FilterRegistrationBean<CSRFFilter> csrfFilter() {
        FilterRegistrationBean<CSRFFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CSRFFilter());
        registration.addUrlPatterns("/*");
        registration.setName("csrfFilter");
        registration.setOrder(2);
        return registration;
    }

    /**
     * 限流过滤器 - 防止DoS攻击
     */
    @Bean
    public FilterRegistrationBean<RateLimitFilter> rateLimitFilter() {
        FilterRegistrationBean<RateLimitFilter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new RateLimitFilter());
        registration.addUrlPatterns("/*");
        registration.setName("rateLimitFilter");
        registration.setOrder(3);
        return registration;
    }
}

